using System;
using System.Collections.Generic;
using System.Text;
using MoGameLib;
using MoEngine;
using Microsoft.Xna.Framework;

namespace MoGameLib
{
    public class MapPickEventArgs
    {
        public BattleTile Tile;
        public Battler Battler;
        public MouseButtons Button;
    }

    public delegate void MapPickEvent(MapPickEventArgs evtArgs);

    public class WindowBattleMap : WindowBoard
    {
        public WindowBattleMap(WindowBase parent)
            : base(parent)
        {
            m_Camera = new Camera2D();
        }
/*
        protected override void OnLayoutValidated()
        {
            base.OnLayoutValidated();

            m_Camera.Center = new Vector3(m_Viewport.Width / 2 + 80, m_Viewport.Height / 2 + 80, 0);
        }*/

        public BattleMap BattleMap
        {
            get { return m_Board as BattleMap; }
        }

        Battler m_LastFocusedBattler;
        BattleTile m_LastOverTile;

        protected Battler FocusedBattler
        {
            get { return m_LastFocusedBattler; }
            set
            {
                m_LastFocusedBattler = value;
                BattleMap.FocusedBattler = value;
            }
        }

        public override void OnMouseMove(Point mousePos, InputEventArgs evtArgs)
        {
            base.OnMouseMove(mousePos, evtArgs);

            m_LastOverTile = BattleMap.GetPickedTile(m_Camera, new Vector2(mousePos.X, mousePos.Y));
            FocusedBattler = m_LastOverTile != null ? m_LastOverTile.Battler : null;
        }

        public event MapPickEvent MapPicked;

        public override void OnMouseDown(MouseButtons button, Point mousePos, InputEventArgs evtArgs)
        {
            base.OnMouseDown(button, mousePos, evtArgs);
            OnPick(button, mousePos);

        }

        public override void OnMouseUp(MouseButtons button, Point mousePos, InputEventArgs evtArgs)
        {
            base.OnMouseUp(button, mousePos, evtArgs);
            OnPick(button, mousePos);
        }

        protected void OnPick(MouseButtons button, Point mousePos)
        {
            if (MapPicked != null)
            {
                BattleTile tile = BattleMap.GetPickedTile(m_Camera, new Vector2(mousePos.X, mousePos.Y));

                if (tile == null)
                    return;

                MapPickEventArgs NewEvtArgs = new MapPickEventArgs();
                NewEvtArgs.Tile = tile;
                NewEvtArgs.Battler = tile.Battler;
                NewEvtArgs.Button = button;

                MapPicked(NewEvtArgs);
            }
        }


        public void SelectBattler(Battler battler)
        {
            BattleMap.SelectedBattler = battler;
        }
        
    }
}
